Attribute VB_Name = "mdlCommon"
Option Explicit

Public Declare Function GetTempPath Lib "kernel32.dll" Alias "GetTempPathA" (ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long
Public Declare Function DeleteFile Lib "kernel32.dll" Alias "DeleteFileA" (ByVal lpFileName As String) As Long
Public Declare Function URLDownloadToFile Lib "urlmon.dll" Alias "URLDownloadToFileA" (ByVal pCaller As Long, ByVal szURL As String, ByVal szFileName As String, ByVal dwReserved As Long, ByVal lpfnCB As Long) As Long
Public Declare Function CopyFile Lib "kernel32.dll" Alias "CopyFileA" (ByVal lpExistingFileName As String, ByVal lpNewFileName As String, ByVal bFailIfExists As Long) As Long
Public Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long

Public Const SW_SHOW = 5

Public Const strBCEPath = "bce.exe"
Public Const strUpdateSetup = "http://bce.googlecode.com/svn/upgrade/setup.dat"
Public Const strUpdateSetupFile = "kernel.ver"
Public Const strUpdateKernel = "http://bce.googlecode.com/svn/upgrade/kernel.dat"
Public Const strUpdateKernelFile = "bce.exe"
Public Const strOnlineDoc = "http://code.google.com/p/bce/w/list"

Public Function CompareFile(ByVal szFile1 As String, ByVal szFile2 As String) As Boolean
    On Error Resume Next
    Dim hFile1 As Long, hFile2 As Long
    Dim chIn1 As Byte, chIn2 As Byte
    If FileLen(szFile1) <> FileLen(szFile2) Then
        CompareFile = False
        Exit Function
    End If
    hFile1 = FreeFile
    Open szFile1 For Binary As hFile1
    hFile2 = FreeFile
    Open szFile2 For Binary As hFile2
    Do
        If EOF(hFile1) = True Or EOF(hFile2) = True Then
            Exit Do
        End If
        Get hFile1, , chIn1
        Get hFile2, , chIn2
        If chIn1 <> chIn2 Then
            CompareFile = False
            Close hFile1
            Close hFile2
            Exit Function
        End If
    Loop
    Close hFile1
    Close hFile2
    CompareFile = True
End Function

Public Function IntoPath(ByVal strSourceDirectory As String, ByVal strFilename As String) As String
    On Error Resume Next
    If Right(strSourceDirectory, 1) = "\" Then
        IntoPath = strSourceDirectory & strFilename
    Else
        IntoPath = strSourceDirectory & "\" & strFilename
    End If
End Function

Public Function GetTemporaryPath() As String
    On Error Resume Next
    Dim sSave As String, lReturn As Long
    sSave = String(255, Chr$(0))
    lReturn = GetTempPath(255, sSave)
    sSave = Left$(sSave, InStr(sSave, Chr$(0)) - 1)
    GetTemporaryPath = sSave
End Function

Public Function IsFileOrDirectoryExist(ByVal sPath As String, Optional bDirectory As Boolean = False) As Boolean
    On Error Resume Next
    Dim sReturn As String
    sReturn = vbNullString
    If bDirectory = True Then
        sReturn = Dir(sPath, vbSystem + vbReadOnly + vbHidden + vbDirectory)
    Else
        sReturn = Dir(sPath, vbSystem + vbReadOnly + vbHidden)
    End If
    If sReturn = vbNullString Then
        IsFileOrDirectoryExist = False
    Else
        IsFileOrDirectoryExist = True
    End If
End Function

Public Sub ResolveCommand(ByVal sCommand As String, ByRef sSliced As Collection)
    On Error Resume Next
    Dim sLocal As String
    Dim lPrevious As Long
    Dim lCurrent As Long
    Dim lFirst As Long
    Dim sTemporary As String
    lPrevious = 1
    sLocal = Trim(sCommand)
    If Left(sLocal, 1) = ";" Then
        Exit Sub
    End If
ReRouteString:
    If Left(sLocal, 1) = "|" Then
        sLocal = Right(sLocal, Len(sLocal) - 1)
        GoTo ReRouteString
    End If
    If Right(sLocal, 1) = "|" Then
        sLocal = Left(sLocal, Len(sLocal) - 1)
        GoTo ReRouteString
    End If
    Do
        lFirst = InStr(1, sLocal, "|")
        If lFirst = 0 Then
            sSliced.Add sLocal
            Exit Do
        End If
        sTemporary = Mid(sLocal, lPrevious, lFirst - lPrevious)
        sSliced.Add sTemporary
        sLocal = Right(sLocal, Len(sLocal) - lFirst)
        DoEvents
    Loop
    DoEvents
End Sub

Public Sub Main()
    On Error Resume Next
    Dim sTitle As String
    If App.PrevInstance = True Then
        sTitle = App.Title
        App.Title = App.CompanyName & " : " & App.EXEName
        AppActivate sTitle
        End
    End If
    Load frmMain
    frmMain.Show
End Sub
